package com.zwy.leetcode;

import java.util.ArrayList;
import java.util.List;

public class Day14Test {
    /**
     * 688. “马”在棋盘上的概率
     * @param n
     * @param k
     * @param row
     * @param column
     * @return
     */
    public double knightProbability(int n, int k, int row, int column) {
        if(k==0)return 1.0;
        int count=0;
        Double p=0.0;
        if(k<0)return 0;
        k--;
        if(isSafe(n,row+1,column+2)){
            count++;
            if(k==0)p+=1;
            else
            p+=knightProbability(n,k,row+1,column+2);
        }
        if(isSafe(n,row+2,column+1)){
            count++;
            if(k==0)p+=1;
            else
            p+=knightProbability(n,k,row+2,column+1);
        }
        if(isSafe(n,row+2,column-1)){
            count++;
            if(k==0)p+=1;
            else
            p+=knightProbability(n,k,row+2,column-1);
        }
        if(isSafe(n,row+1,column-2)){
            count++;
            if(k==0)p+=1;
            else
            p+=knightProbability(n,k,row+1,column-2);
        }
        if(isSafe(n,row-1,column-2)){
            count++;
            if(k==0)p+=1;
            else
            p+=knightProbability(n,k,row-1,column-2);
        }
        if(isSafe(n,row-2,column-1)){
            count++;
            if(k==0)p+=1;
            else
            p+=knightProbability(n,k,row-2,column-1);
        }
        if(isSafe(n,row-2,column+1)){
            count++;
            if(k==0)p+=1;
            else
            p+=knightProbability(n,k,row-2,column+1);
        }
        if(isSafe(n,row-1,column+2)){
            count++;
            if(k==0)p+=1;
            else
            p+=knightProbability(n,k,row-1,column+2);
        }
        if(count==0)return 0;
        if(p==0)p=count*1.0;
        return count/8.0*(p/count);
    }
    public boolean isSafe(int n,int row,int column){
        if(row<n&&row>=0&&column<n&&column>=0)return true;
        return false;
    }

    /**
     *
     * @param s
     * @return
     */
    public String replaceSpace(String s) {
        List<String> list=new ArrayList<>();
        String res="";
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i)==' '){
                String substring = s.substring(0, i);
                s=s.substring(i+1,s.length());
               list.add(substring);
            }
        }
        for (String s1 : list) {
            if(" ".equals(s1))
                res+="%20";
            else
            res+=s1+"%20";
        }
        System.out.println(list);
        if(" ".equals(s))return  res+"%20";
        return res+s;
    }
    public static void main(String[] args) {
        int n=9;
        int k=2;
        int row=1;
        int column=1;
        String s="     ";
        Day14Test test=new Day14Test();
        String s1 = test.replaceSpace(s);
        System.out.println(s1);
    }
}
